//
//  RocAlertView.m
//  RocRemoteControlProject
//
//  Created by 新致李照鹏 on 2018/12/28.
//  Copyright © 2018年 Roc. All rights reserved.
//

#import "RocAlertView.h"

@interface RocAlertView ()

@property (nonatomic, copy) RocAlertViewCallBack callBack;

@property (nonatomic, strong) UILabel *tipsLabel;

@end

@implementation RocAlertView


+ (instancetype)ShowInView:(UIView *)view Tips:(NSString *)tips CallBack:(RocAlertViewCallBack)callBack;
{
    RocAlertView *alert = [[RocAlertView alloc] initWithFrame:view.bounds];
    alert.tipsLabel.text = tips;
    alert.callBack = callBack;
    [view addSubview:alert];
    
    return alert;
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        [self buildBGView];
        
        [self buildAlertView];
    }
    return self;
}
- (void)dealloc
{
    NSLog(@"RocAlertView -- 销毁了");
}

- (void)buildBGView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    imageView.backgroundColor = [UIColor blackColor];
    imageView.alpha = 0.3;
    [self addSubview:imageView];
}

- (void)buildAlertView
{
    CGFloat width = KROCConver_750(500);
    CGFloat height = KROCConver_750(300);
    
    CGRect rect = CGRectMake(0, 0, width, height);
    UIView *view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = [UIColor whiteColor];
    view.center = self.center;
    view.layer.cornerRadius = 8.0f;
    view.layer.borderColor = [UIColor lightGrayColor].CGColor;
    view.layer.borderWidth = 1.0f;
    
    CGFloat space = KROCConver_750(30);
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(space, space, width-space*2, KROCConver_750(100))];
    label.font = [UIFont systemFontOfSize:17];
    label.numberOfLines = 0;
    label.textColor = [UIColor colorFromHexString:@"#4b4b4b"];
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(space, height-KROCConver_750(100), KROCConver_750(270), KROCConver_750(60))];
    button.clipsToBounds = YES;
    button.layer.cornerRadius = KROCConver_750(60)/2;
    button.backgroundColor = [UIColor colorFromHexString:@"#0ae38f"];
    [button setTitle:@"确认" forState:UIControlStateNormal];
    [view addSubview:button];
    
    UIButton *cancel = [[UIButton alloc] initWithFrame:CGRectMake(width-space-KROCConver_750(130), height-KROCConver_750(100), KROCConver_750(130), KROCConver_750(60))];
    cancel.clipsToBounds = YES;
    cancel.layer.cornerRadius = KROCConver_750(60)/2;
    cancel.backgroundColor = [UIColor colorFromHexString:@"#fc837b"];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [view addSubview:cancel];
    
    [button addTarget:self action:@selector(completeButton) forControlEvents:UIControlEventTouchUpInside];
    [cancel addTarget:self action:@selector(cancelButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:view];
    
    self.tipsLabel = label;
}
- (void)completeButton
{
    [self removeFromSuperview];
    self.callBack(YES);
    
}
- (void)cancelButton
{
    [self removeFromSuperview];
    self.callBack(NO);
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
